package cpalinesearch;

import java.util.Vector;

import cpagraph.Bus;
import cpagraph.Edge;
import cpagraph.Node;

/**
 * @author Riva
 *
 */
public class LineConstructor {
	
	/**
	 * 
	 */
	String actual_line;
	
	/**
	 * 
	 */
	Vector<Node> node_list = null;
	
	/**
	 * 
	 */
	Vector<String> path=null;
	
	/**
	 * 
	 */
	int num_jumps;

	/**
	 * @param node_list
	 */
	public LineConstructor(Vector<Node> node_list)
	{
		this.node_list = node_list;
		num_jumps = node_list.size()-1;
		path = new Vector(num_jumps);
	}
	
	/**
	 * @param a
	 * @param b
	 * @return
	 */
	public String busFinder(Node a, Node b)
	{
		String line = null;
		Edge ab = a.getEdge(b);
		if ((ab == null))
		{
			return line;
		}
		
		//first aproximation: it only looks for the bus that is in both nodes
		for (int i = 0; i < a.busses.size(); i++)
		{
			Bus bus = a.busses.elementAt(i);
			if (b.isAssignedTo(bus))
			{
				line = bus.number;
				return line;
			}
		}
		return line;
	}
	
	/**
	 * 
	 */
	public void pathFinder()
	{	
		actual_line = null;
		Bus auxiliaryLine = new Bus("-1");
		//we go throw all the jumps between nodes
		for(int i=0; i == num_jumps; i++){
			if (actual_line == null)
			{
				//first link: we find a common bus line
				actual_line = busFinder (node_list.elementAt(0),node_list.elementAt(1));
			}
			else
			{
				auxiliaryLine.setBus(actual_line);
				//if the bus line exists also at the next node, we don't transfer
				if (node_list.elementAt(i+1).isAssignedTo(auxiliaryLine)){
					//do nothing
				}
				//if the bus line is not at the next node, we do transfer
				else
				{
					actual_line = busFinder(node_list.elementAt(i), node_list.elementAt(i+1));
				}
			}
			//we add the line we have chosen to the path
			path.addElement(actual_line);
		}
	}
}
